<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 19:28
 */

namespace app\common\model\mysql;


use think\Model;

class Config extends Model
{
    protected $pk = 'config_id';

    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time'];

    public function getConfigInfoAttr($value)
    {
        $info   = explode(',', $value);   // [1|开启, 0|关闭]
        $extend = [];
        foreach ($info as $k => $v) {
            $data = explode("|", $v);   // [[1,开启],[0,关闭]]

            //            [
            //                0 => [
            //                    0 => 1,
            //                    1 => '开启'
            //                ],
            //                1 => [
            //                    0 => 0,
            //                    1 => '关闭'
            //                ]
            //            ]
            foreach ($data as $k1 => $v1) {
                $extend[$k][$k1] = $data[$k1];
            }

            //            $extend[$data[0]] = $data;
        }

        return $extend;
    }

    public function add($data)
    {
        if (empty($data) || !is_array($data)) {
            return false;
        }

        // todo: 暂时是在写入一遍配置文件，这里有IO，后面换成缓存
        $this->putConfigFile();

        return $this->save($data);
    }

    public function saveConfig($config_id, $data)
    {
        $res = $this->where('config_id', $config_id)->save($data);
        $this->putConfigFile();

        return $res;
    }

    public function putConfigFile()
    {
        $page_data = $this->order('config_id asc')->select()->toArray();
        $arr       = [];
        if (!empty($page_data)) {
            foreach ($page_data as $k => $v) {
                $name = strtoupper($v['config_name']);
                if (strtoupper($v['config_value']) == "FALSE") {
                    $v['config_value'] = false;
                }
                if (strtoupper($v['config_value']) == "TRUE") {
                    $v['config_value'] = true;
                }
                $arr[$name] = htmlspecialchars_decode($v['config_value']);
            }
        }
        // 写入配置文件
        $content = "<?php return ".var_export($arr, true)."\n?>";

        return file_put_contents("../config/site.php", $content);
    }

    /**
     * @desc 根据group_id 查找对应的配置组下的所有配置
     * @param $id
     * @return bool|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllConfigsByGroupId($id)
    {
        $group_id = intval($id);
        if (empty($group_id)) {
            return false;
        }

        $where = [
            'group_id' => $group_id,
        ];

        return $this->where($where)->select();
    }

    public function getOneConfigByConfigId($config_id)
    {
        $config_id = intval($config_id);
        if (empty($config_id)) {
            return false;
        }

        $where = ['config_id' => $config_id];

        return $this->where($where)->find()->getData();
    }
}